﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text.Json;
using System.Text.Json.Serialization;
using System.Threading.Tasks;
using Avalonia.Platform;
using DynamicData;

namespace DevHub.WindowsApp.ViewModels.Pages.Help;

public class FontIconViewModel : PageModelBase
{
    private string _filterText = "";

    public string FilterText
    {
        get => _filterText;
        set
        {
            if (SetField(ref _filterText, value))
            {
                OnPropertyChanged();
            }
        }
    }


    public ObservableCollection<FontIconInfo> Icons { get; set; } = [];

    public FontIconViewModel()
    {
        Init();
    }
    
    private async void Init()
    {
        await Task.Run(() =>
        {
            using var s = AssetLoader.Open(new Uri("avares://DevHub.WindowsApp/Assets/FASymbolFontList.json"));
            var icons = JsonSerializer.Deserialize<List<FontIconInfo>>(s)!;
            Icons.Add(icons);
        });
    }
}

public class FontIconInfo
{
    [JsonConstructor]
    public FontIconInfo(string name, string codepoint)
    {
        Name = name;
        Codepoint = codepoint;
        XamlGlyph = $"&#x{codepoint};";
        CSharpGlyph = $"\\u{codepoint}";

        XamlExample = @"<ui:FontIcon Glyph=""" + XamlGlyph + @""" />";
        CSharpExample = "FontIcon fontIcon = new FontIcon()" + Environment.NewLine +
                        @"fontIcon.Glyph = """ + CSharpGlyph + @""";";

        Glyph = char.ConvertFromUtf32((int)Convert.ToUInt32(codepoint, 16));
    }

    public string Name { get; set; }

    public string Codepoint { get; set; }

    public string Glyph { get; }

    [JsonIgnore] public string XamlGlyph { get; }

    [JsonIgnore] public string CSharpGlyph { get; }

    [JsonIgnore] public string XamlExample { get; }

    [JsonIgnore] public string CSharpExample { get; }
}